//
//  BIZLoginViewController.m
//  jiedabao
//
//  Created by 小龙 on 2017/3/15.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZLoginViewController.h"
#import <Bugly/Bugly.h>
#import "AppDelegate.h"

@interface BIZLoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *accountTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;

@end

@implementation BIZLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = HEXCOLOR(0xF9F9F9);
//    self.accountTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.accountTextField.placeholder attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
//    
//    self.pwdTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.pwdTextField.placeholder attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}


- (IBAction)loginBtnClicked:(UIButton *)sender {
    
    [_accountTextField resignFirstResponder];
    [_pwdTextField resignFirstResponder];
    
    
    if (!_accountTextField.text.length) {
        [MBProgressHUD showTipMessageInWindow:@"请输入账号或手机号"];
        return;
    }
    
    if (!_pwdTextField.text.length ) {
        [MBProgressHUD showTipMessageInWindow:@"请输入密码"];
        return;
    }
    
    NSDictionary *parameters = @{
                                 @"accountId" : _accountTextField.text,
                                 @"pwd" : [_pwdTextField.text hyb_toMD5],
                                 @"deviceType" : @0
                                 };
    
    
    
    [MBProgressHUD showAnimationHUD];
    [RequestTool POST:kLoginUrl
           parameters:parameters
             progress:nil
              success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                  int code = [responseObject[@"code"] intValue];
                  if (code == 1) {
                      [MBProgressHUD showSuccessMessage:responseObject[@"des"]];
                      [self loginSuccessRespnse:responseObject];
                  } else {
                      [MBProgressHUD showErrorMessage:responseObject[@"des"]];
                  }
              }
              failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                  DeLog(@"登录error:%@",error);
                  [Bugly reportError:error];
                  
                  if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
                      [MBProgressHUD showErrorMessage:@"网络不给力"];
                  }
                  else {
                      [MBProgressHUD showErrorMessage:@"服务器繁忙"];
                  }

              }
     ];
}

- (void)loginSuccessRespnse:(id _Nullable)respon
{
   
    NSLog(@"respon %@", respon);
    
    NSDictionary *data = respon[@"data"];
    if (data) {
        NSString *cookieID = data[@"cookieId"];
        [BIZUserModel shareUser].cookieID = cookieID;
        [NetManager refreshCookieId:cookieID];
        [BIZUserModel shareUser].name = data[@"name"];
        [BIZUserModel shareUser].city = data[@"city"];
        [BIZUserModel shareUser].login = YES;
        [BIZUserModel shareUser].detailModel = [BIZUserDetailModel yy_modelWithJSON:data];
        [self bindAliasWithAccout:data[@"uuid"]];
    }
    
    if (self.isNotLogin) {
        AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
        [appDelegate configRootViewController];
    } else {
        if (self.loginSuccessBlock) {
            self.loginSuccessBlock();
        }
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    
    
    
}

// 使用帐号绑定alias
- (void)bindAliasWithAccout:(NSString *)uuid {
    
    if (uuid.length) {
        
        dispatch_queue_t queue = dispatch_queue_create(0, 0);
        dispatch_async(queue, ^{
            
            [UMessage setAlias:uuid type:@"alias" response:^(id  _Nonnull responseObject, NSError * _Nonnull error) {
                
                if (error) {
                    DeLog(@"set alias err %@", error);
                    
                }else {
                    NSLog(@"set alias successfully--%@" ,responseObject);
                }
                
            }];
            
        });
    }

}



@end
